.\" Copyright (c) 2011 Kai Wang
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $Id: dwarf_get_relocation_info.3 3644 2018-10-15 19:55:01Z jkoshy $
.\"
.Dd September 3, 2011
.Dt DWARF_GET_RELOCATION_INFO 3
.Os
.Sh NAME
.Nm dwarf_get_relocation_info
.Nd retrieve generated relocation arrays
.Sh LIBRARY
.Lb libdwarf
.Sh SYNOPSIS
.In libdwarf.h
.Ft int
.Fo dwarf_get_relocation_info
.Fa "Dwarf_P_Debug dbg"
.Fa "Dwarf_Signed *elf_section_index"
.Fa "Dwarf_Signed *elf_section_link"
.Fa "Dwarf_Unsigned *reloc_entry_count"
.Fa "Dwarf_Relocation_Data *reloc_buf"
.Fa "Dwarf_Error *err"
.Fc
.Sh DESCRIPTION
The function
.Fn dwarf_get_relocation_info
is used to retrieve the relocation arrays generated by a prior call to
.Xr dwarf_transform_to_disk_form 3 .
.Pp
Each call to this function retrieves the next available relocation
array.
Application code should call this function repeatly to retrieve all
the relocation arrays.
The total number of generated relocation arrays retrievable
by this function may be obtained by calling function
.Xr dwarf_get_relocation_info_count 3 .
.Pp
Argument
.Ar dbg
should reference a DWARF producer instance allocated using
.Xr dwarf_producer_init 3 in sequence.
or
.Xr dwarf_producer_init_b 3 .
The
.Dv DW_DLC_SYMBOLIC_RELOCATIONS
flag should have been set on the DWARF producer instance.
.Pp
Argument
.Ar elf_section_index
should point to a location which will be set to the ELF section index
of the relocation section to which the retrieved relocation array
belongs.
.Pp
Argument
.Ar elf_section_link
should point to a location which will be set to the section index of
the ELF section to which the retrieved relocation array applies.
.Pp
Argument
.Ar reloc_entry_count
should point to a location which will be set to the total number of
relocation entries contained in the relocation array.
.Pp
Argument
.Ar reloc_buf
should point to a location which will be set to a pointer to the
retrieved array of relocation entries.
.Pp
If argument
.Ar err
is not NULL, it will be used to store error information in case
of an error.
.Pp
The retrieved relocation entries are described using structure
.Vt Dwarf_Relocation_Data_s ,
defined in the header file
.In libdwarf.h :
.Bd -literal -offset indent
typedef struct Dwarf_Relocation_Data_s {
	unsigned char drd_type;
	unsigned char drd_length;
	Dwarf_Unsigned drd_offset;
	Dwarf_Unsigned drd_symbol_index;
} *Dwarf_Relocation_Data;
.Ed
.Pp
Struct
.Vt Dwarf_Relocation_Data_s
consists of following fields:
.Bl -tag -width ".Va drd_symbol_index" -compact -offset indent
.It Va drd_type
The type code of the relocation entry.
The
.Vt Dwarf_Rel_Type
enumeration defined in the header file
.In libdwarf.h
specifies legal values for this field.
.It Va drd_length
The size in bytes of the field to be relocated.
.It Va drd_offset
The section-relative offset of the field to be relocated.
.It Va drd_symbol_index
The symbol index associated with the relocation entry.
.El
.Ss Memory Management
The memory area used for the relocation arrays is managed by the
.Lb libdwarf .
The function
.Fn dwarf_producer_finish
may be used to release it, along with other resources associated
with the producer instance.
.Sh RETURN VALUES
On success, function
.Fn dwarf_get_relocation_info
returns
.Dv DW_DLV_OK .
It returns
.Dv DW_DLV_NO_ENTRY
if there were no more relocation arrays to retrieve, or if the flag
.Dv DW_DLC_SYMBOLIC_RELOCATIONS
was not set on the producer instance.
In case of an error, function
.Fn dwarf_get_relocation_info
returns
.Dv DW_DLV_ERROR
and sets the argument
.Ar err .
.Sh EXAMPLES
To generate relocation entries and retrieve them, use:
.Bd -literal -offset indent
Dwarf_P_Debug dbg;
Dwarf_Relocation_Data buf;
Dwarf_Signed count, index, link;
Dwarf_Unsigned reloc_cnt, entry_cnt;
Dwarf_Error de;
int version, i, j;

/*
 * Assume that dbg refers to a DWARF producer instance created
 * created with DW_DLC_SYMBOLIC_RELOCATIONS flag set and that
 * application code has added DWARF debugging information
 * to the producer instance.
 */
if ((count = dwarf_transform_to_disk_form(dbg, &de)) ==
    DW_DLV_NOCOUNT) {
	warnx("dwarf_transform_to_disk_form failed: %s",
	    dwarf_errmsg(-1));
	return;
}

/* ... process generated section byte streams ... */
if (dwarf_get_relocation_info_count(dbg, &reloc_cnt, &version, &de) !=
    DW_DLV_OK) {
	warnx("dwarf_get_relocation_info_count failed: %s",
	    dwarf_errmsg(-1));
	return;
}

for (i = 0; (Dwarf_Unsigned) i < reloc_cnt; i++) {
	if (dwarf_get_relocation_info(dbg, &index, &link, &entry_cnt,
	    &buf, &de) != DW_DLV_OK) {
		warnx("dwarf_get_relocation_info failed: %s",
		    dwarf_errmsg(-1));
		continue;
	}
	for (j = 0; (Dwarf_Unsigned) j < entry_cnt; j++) {
		/* ...use each reloc data in buf[j]... */
	}
}

dwarf_producer_finish(dbg, &de);
.Ed
.Sh ERRORS
Function
.Fn dwarf_get_relocation_info
can fail with:
.Bl -tag -width ".Bq Er DW_DLE_NO_ENTRY"
.It Bq Er DW_DLE_ARGUMENT
One of the arguments
.Ar dbg ,
.Ar elf_section_index ,
.Ar elf_section_link ,
.Ar reloc_entry_count
or
.Ar reloc_buf
was NULL.
.It Bq Er DW_DLE_NO_ENTRY
There were no more ELF relocation arrays to retrieve.
.It Bq Er DW_DLE_NO_ENTRY
The flag
.Dv DW_DLC_SYMBOLIC_RELOCATIONS
was not set on the producer instance.
.It Bq Er DW_DLE_NO_ENTRY
Function
.Xr dwarf_transform_to_disk_form 3
was not called prior to calling function
.Fn dwarf_get_relocation_info .
.El
.Sh SEE ALSO
.Xr dwarf 3 ,
.Xr dwarf_get_relocation_info_count 3 ,
.Xr dwarf_producer_finish 3 ,
.Xr dwarf_producer_init 3 ,
.Xr dwarf_producer_init_b 3 ,
.Xr dwarf_reset_section_bytes 3 ,
.Xr dwarf_transform_to_disk_form 3
